package org.ecype.diego;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Annotations {

	private Annotations() {
	}

	public static <T extends Annotation> List<Class<?>> extractAnnotatedClasses(Class<?> container, Class<T> annotationClass) {
		List<Class<?>> classes = new ArrayList<Class<?>>();
		for (Class<?> c : container.getDeclaredClasses()) {
			T annotation = c.getAnnotation(annotationClass);
			if (annotation != null) {
				classes.add(c);
			}
		}
		return classes;
	}

	public static <T extends Annotation> List<Field> extractAnnotatedFields(Class<?> container, Class<T> annotationClass) {
		List<Field> fields = new ArrayList<Field>();
		for (Field f : container.getFields()) {
			T annotation = f.getAnnotation(annotationClass);
			if (annotation != null) {
				fields.add(f);
			}
		}

		for (Field f : container.getDeclaredFields()) {
			T annotation = f.getAnnotation(annotationClass);
			if (annotation != null && !fields.contains(f)) {
				fields.add(f);
			}
		}
		return fields;
	}

}
